﻿using Microsoft.Extensions.Hosting;
using System.IO.Pipes;

namespace ConsoleTest.Services
{
    public class NamedPipeStreamService : IHostedService
    {
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            using NamedPipeServerStream server = new("ServerName", PipeDirection.Out);
            using NamedPipeClientStream client = new(".", "ServerName", PipeDirection.In);
            await client.ConnectAsync(cancellationToken);
            await server.WaitForConnectionAsync(cancellationToken);
            using StreamWriter writer = new(server);
            await writer.WriteAsync("Hello, client!");
            await writer.FlushAsync();
        }

        public async Task StopAsync(CancellationToken cancellationToken) => await Task.CompletedTask;
    }
}
